app.controller('eventsubscriptionscontroller', ['$scope', '$parse', '$http', function($scope, $parse, $http)
{
	$scope.$on('translations', function(event, args) {
		$scope.translations = args.translations.response;
		$scope.loadEventSubscriptionsComponentCommand();
		$scope.getAllEventSubscriptionStatesCommand();
	});

	/**
	* Update the event command
	*/
	$scope.updateEventCommand = function()
	{
		console.info("updateEventCommand - called"); 
		
		try
		{
			if($scope.editEventTitle == "")
			{
				throw "Le titre de l'événement est obligatoire.";
			}
			
			$scope.editEventNumberOfPlaces = $scope.editIsUnlimitedPlaces ? -1 : $('#editNumberOfPlaces').val();

			$scope.editStartDate = $('#controlEditStartDate').val();
			$scope.editEndDate = $('#controlEditEndDate').val();
			var state = $scope.eventStates.indexOf($scope.editEventState);

			$http.post($scope.servicebase, { "service" : "updateEvent", "id" : $scope.editEventId, "title" : $scope.editEventTitle , "description" : $scope.editEventDescription, "startDate" : $scope.editStartDate, "endDate" : $scope.editEndDate, "numberOfPlaces" : $scope.editEventNumberOfPlaces, "archiveName" : $scope.editArchiveReference.Name, "state" : state }).
			success(function(data, status) 
			{
				console.info("updateEventCommand - call success"); 
				$scope.status = status;
				$scope.data = data;
				$scope.result = data; 

				if(data.isFailed == true)
				{
					$scope.messageUpdateEventVisible = true;
					$scope.setMessageCommand($scope.data.exception, 'danger', 'messageUpdateEvent');
				}
				else
				{
					if($scope.Poster != null)
					{
						$scope.attachPosterToEventCommand(data.response.Id);
					}
				
					$("#editEvent").modal('hide');
					$scope.getEventsCommand();
					$scope.setMessageCommand("L'événement #" + $scope.editEventId + " a été mis à jour." , 'success');
				}
			})
			.
			error(function(data, status) 
			{
				console.error("updateEventCommand - call failed"); 	
				$scope.data = data || "Request failed";
				$scope.status = status;			
				console.error("updateEventCommand - data : " + $scope.data); 				
			});
		}
		catch(err) 
		{
			$scope.messageUpdateEventVisible = true;
			$scope.setMessageCommand(err, 'danger', 'messageUpdateEvent');
		}
	};
	
	/**
	* Update the event subscription state
	*/
	$scope.updateEventSubscriptionCommand = function(eventsubscription)
	{
		console.info("updateEventSubscriptionCommand - called");
		
		var state = $scope.eventSubscriptionsStates.indexOf(eventsubscription.State);
		
		$http.post($scope.servicebase, { "service" : "updateEventSubscription", "id" : eventsubscription.Id, "state" : state}).
		success(function(data, status) 
		{
			console.info("updateEventSubscription - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if($scope.data.isFailed == true)
			{
				$scope.eventSubscriptionMessageVisible = true;
				$scope.setMessageCommand($scope.data.exception, 'danger', 'eventSubscriptionMessage');
			}
			else
			{
				$scope.getEventSubscriptionsToValidCommand();
				$scope.eventSubscriptionMessageVisible = true;
				$scope.setMessageCommand('L\'état de la pré-inscription été modifié', 'info', 'eventSubscriptionMessage');
			}
		})
		.
		error(function(data, status) 
		{
			console.error("updateEventSubscription - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;
			console.error("updateEventSubscription - data : " + $scope.data);		
		});
	}
	
	/**
	* Get all states event subscription
	*/
	$scope.getAllEventSubscriptionStatesCommand = function()
	{
		console.info("getAllEventSubscriptionStatesCommand - called");
		
		$scope.eventSubscriptionsStates = [];
		
		$http.post($scope.servicebase, { "service" : "getEventSubscriptionStates"}).
		success(function(data, status) 
		{
			console.info("getEventSubscriptionStates - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data;
			if($scope.data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			else
			{
				for(var state in $scope.data.response) {
				   $scope.eventSubscriptionsStates.push($scope.translations["EventSubscriptions_State_" + state]);
				}
			}
		})
		.
		error(function(data, status) 
		{
			console.error("getEventSubscriptionStates - call failed"); 	
			$scope.data = data || "Request failed";
			$scope.status = status;
			console.error("getEventSubscriptionStates - data : " + $scope.data);		
		});
	};

	/**
	* Load the event subscription component
	*/
	$scope.loadEventSubscriptionsComponentCommand = function() 
	{
		console.info("loadEventSubscriptionsComponentCommand - called");
		$scope.eventSubscriptionState = '';
	};
	
	$scope.loadEventSubscriptionsComponentCommand();
}]);